home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware in MacFormat / brailler0.5b / brlr ƒ / Shell ƒ / graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  7.5 KB  |  290 lines  |  [TEXT/MMCC]

  1. #include "graphics.h"
  2. #include "graphics dispatch.h"
  3. #include "window layer.h"
  4. #include <QDOffscreen.h>
  5.  
  6. extern    Boolean            gHasColorQD;    /* see environment.c */
  7.  
  8. static    void GetMainScreenBounds(Rect *screenRect);
  9. static    short GetBiggestDeviceDepth(WindowPtr theWindow);
  10. static    short GetWindowRealDepth(WindowPtr theWindow);
  11.  
  12. OSErr OpenTheIndWindow(short index)
  13. {
  14.     WindowPtr        theWindow;
  15.     Point            topLeft;
  16.     ExtendedWindowPtr    storage;
  17.     Rect            screenRect;
  18.     Rect            boundsRect;
  19.     short            windowType;
  20.     
  21.     if (!IndWindowExistsQQ(index))
  22.     {
  23.         storage=(ExtendedWindowPtr)NewPtr(sizeof(ExtendedWindowRec));
  24.         if (storage==0L)
  25.             return MemError();
  26.         
  27.         theWindow=(WindowPtr)storage;
  28.         SetupWindowDispatch(index, theWindow);
  29.         windowType=GetWindowType(theWindow);
  30.         
  31.         if (WindowAutoCenterQQ(theWindow))
  32.         {
  33.             GetMainScreenBounds(&screenRect);
  34.             topLeft.h=screenRect.left+
  35.                 (((screenRect.right-screenRect.left)-GetWindowWidth(theWindow))/2);
  36.             topLeft.v=screenRect.top+
  37.                 (((screenRect.bottom-screenRect.top)-GetWindowHeight(theWindow))/2);
  38.             SetWindowTopLeft(theWindow, topLeft);
  39.             if ((windowType==noGrowDocProc) || (windowType==documentProc) ||
  40.                 (windowType==movableDBoxProc) || (windowType==zoomDocProc) ||
  41.                 (windowType==zoomNoGrow) || (windowType==rDocProc))
  42.             {
  43.                 topLeft.v+=9;
  44.             }
  45.         }
  46.         else
  47.         {
  48.             topLeft=GetWindowTopLeft(theWindow);
  49.         }
  50.         
  51.         if (topLeft.v<GetMBarHeight()+1)
  52.             topLeft.v=GetMBarHeight()+1;
  53.         
  54.         SetRect(&boundsRect, topLeft.h, topLeft.v, topLeft.h+GetWindowWidth(theWindow),
  55.             topLeft.v+GetWindowHeight(theWindow));
  56.         SetWindowBounds(theWindow, boundsRect);
  57.         
  58.         if (gHasColorQD)
  59.         {
  60.             if (WindowIsFloatQQ(theWindow))
  61.             {
  62.                 theWindow=MyNewFloatCWindow(storage, &(GetWindowBounds(theWindow)),
  63.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  64.                     WindowHasCloseBoxQQ(theWindow), 0L);
  65.             }
  66.             else
  67.             {
  68.                 theWindow=MyNewCWindow(storage, &(GetWindowBounds(theWindow)),
  69.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  70.                     WindowHasCloseBoxQQ(theWindow), 0L);
  71.             }
  72.         }
  73.         else
  74.         {
  75.             if (WindowIsFloatQQ(theWindow))
  76.             {
  77.                 theWindow=MyNewFloatWindow(storage, &(GetWindowBounds(theWindow)),
  78.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  79.                     WindowHasCloseBoxQQ(theWindow), 0L);
  80.             }
  81.             else
  82.             {
  83.                 theWindow=MyNewWindow(storage, &(GetWindowBounds(theWindow)),
  84.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  85.                     WindowHasCloseBoxQQ(theWindow), 0L);
  86.             }
  87.         }
  88.     }
  89.     
  90.     if (IndWindowExistsQQ(index))
  91.     {
  92.         theWindow=GetIndWindowPtr(index);
  93.         MySelectWindow(theWindow);        /* immediately select this new window */
  94.         SetPort(theWindow);                /* important! for TE info to stick*/
  95.         InvalRect(&(theWindow->portRect));
  96.         /* call window's dispatch routine to alert it that it's open now */
  97.         OpenWindowDispatch(index);
  98.         ActivateWindowDispatch(index);
  99.     }
  100.     else return -1;
  101.     
  102.     return noErr;
  103. }
  104.  
  105. OSErr UpdateTheWindow(WindowPtr theWindow)
  106. {
  107.     short            index;
  108.     long            offRowBytes, sizeOfOff;
  109.     PixMapHandle    thePixMapHandle;
  110.     GWorldPtr        currentGWorld;
  111.     GDHandle        currentGDHandle;
  112.     Rect            boundsRect;
  113.     GWorldPtr        theGWorld;
  114.     Ptr                bwBitMap;
  115.     GrafPort        bwGrafPort;
  116.     GrafPtr            bwGrafPtr;
  117.     OSErr            memError;
  118.     TEHandle        hTE;
  119.     short            realWindowDepth, maxWindowDepth;
  120.     
  121.     index=GetWindowIndex(theWindow);
  122.     realWindowDepth=GetWindowRealDepth(theWindow);
  123.     maxWindowDepth=GetWindowMaxDepth(theWindow);
  124.     boundsRect=theWindow->portRect;
  125.     OffsetRect(&boundsRect, -boundsRect.left, -boundsRect.top);
  126.  
  127.     if (gHasColorQD)    /* w/o Color Quickdraw, GWorlds may not be supported */
  128.     {
  129.         /* try to create new graphics world; display error if unsuccessful */
  130.         if (NewGWorld(&theGWorld, (realWindowDepth>=maxWindowDepth) ? maxWindowDepth : 0,
  131.                 &boundsRect, 0L, 0L, 0)!=0)
  132.         {
  133.             return MemError();
  134.         }
  135.         
  136.         NoPurgePixels(GetGWorldPixMap(theGWorld));    /* never purge our pixmap! */
  137.         
  138.         GetGWorld(¤tGWorld, ¤tGDHandle);    /* get current settings */
  139.         LockPixels(thePixMapHandle=GetGWorldPixMap(theGWorld));    /* important!  copybits may move mem */
  140.         /* update offscreen graphics world, compensating for change in pixel depth */
  141.         UpdateGWorld(&theGWorld, (realWindowDepth>=maxWindowDepth) ? maxWindowDepth : 0,
  142.                 &boundsRect, 0L, 0L, 0);
  143.         SetGWorld(theGWorld, 0L);                /* set to our offscreen gworld */
  144.         
  145.         if ((**(**(GetGWorldDevice(theGWorld))).gdPMap).pixelSize!=GetWindowDepth(theWindow))
  146.             ChangeDepthDispatch(index, GetWindowDepth(theWindow));
  147.     }
  148.     else    /* deal with (guaranteed) B/W bitmaps manually */
  149.     {
  150.         bwGrafPtr=&bwGrafPort;
  151.         OpenPort(bwGrafPtr);
  152.         offRowBytes=(((boundsRect.right-boundsRect.left)+15)>>4)<<1;
  153.         sizeOfOff=(long)(boundsRect.bottom-boundsRect.top)*offRowBytes;
  154.         bwBitMap=NewPtr(sizeOfOff);
  155.         if (bwBitMap==0L)
  156.         {
  157.             memError=MemError();
  158.             ClosePort(bwGrafPtr);
  159.             return memError;
  160.         }
  161.         
  162.         bwGrafPort.portBits.baseAddr=bwBitMap;
  163.         bwGrafPort.portBits.rowBytes=offRowBytes;
  164.         bwGrafPort.portBits.bounds=bwGrafPort.portRect=boundsRect;
  165.         
  166.         SetPort(bwGrafPtr);
  167.     }    
  168.     
  169.     SetWindowDepth(theWindow, (realWindowDepth>maxWindowDepth) ? maxWindowDepth : realWindowDepth);
  170.     if (DrawWindowDispatch(index, GetWindowDepth(theWindow))==kFailure)
  171.     {
  172.         EraseRect(&(theWindow->portRect));
  173.     }
  174.     
  175.     if (gHasColorQD)
  176.         SetGWorld(currentGWorld, currentGDHandle);
  177.     
  178.     SetPort(theWindow);
  179.     
  180.     if (CopybitsDispatch(index, gHasColorQD ? (WindowPtr)theGWorld : (WindowPtr)bwGrafPtr)==kFailure)
  181.     {
  182.         CopyBits(gHasColorQD ?
  183.                     &(((WindowPtr)theGWorld)->portBits) :
  184.                     &(((WindowPtr)bwGrafPtr)->portBits),
  185.                 &(theWindow->portBits),
  186.                 &boundsRect,
  187.                 &boundsRect,
  188.                 0,
  189.                 0L);
  190.         if ((GetWindowVScrollBar(theWindow)!=0L) || (GetWindowHScrollBar(theWindow)!=0L))
  191.             UpdateControls(theWindow, theWindow->visRgn);
  192.     }
  193.     
  194.     if ((hTE=GetWindowTE(theWindow))!=0L)
  195.     {
  196.         EraseRect(&((**hTE).viewRect));
  197.         TEUpdate(&(theWindow->portRect), hTE);
  198.     }
  199.     
  200.     ValidRect(&(theWindow->portRect));
  201.     
  202.     if (gHasColorQD)
  203.     {
  204.         UnlockPixels(thePixMapHandle);
  205.         DisposeGWorld(theGWorld);
  206.     }
  207.     else
  208.     {
  209.         ClosePort(bwGrafPtr);
  210.         DisposePtr(bwBitMap);
  211.     }
  212.     
  213.     CompactMem(maxSize);
  214.     
  215.     return noErr;
  216. }
  217.  
  218. Boolean CloseTheWindow(WindowPtr theWindow)
  219. {
  220.     short            index;
  221.     
  222.     index=GetWindowIndex(theWindow);
  223.     
  224.     if (CloseWindowDispatch(index)==kCancel)
  225.         return FALSE;
  226.     
  227.     DisposeWindowDispatch(index);
  228.     
  229.     MyDisposeWindow(theWindow);
  230.     
  231.     return TRUE;    /* successful close */
  232. }
  233.  
  234. /* -------------------------------------------- */
  235. /* the rest of these are internal to graphics.c */
  236.  
  237. static    void GetMainScreenBounds(Rect *screenRect)
  238. {
  239.     *screenRect = qd.screenBits.bounds;        /* low-mem global */
  240.     (*screenRect).top += GetMBarHeight();    /* don't include menu bar */
  241. }
  242.  
  243. static    short GetBiggestDeviceDepth(WindowPtr theWindow)
  244. {
  245.     Rect            tempRect;
  246.     long            biggestSize;
  247.     long            tempSize;
  248.     GDHandle        thisHandle, gBiggestDevice;
  249.     
  250.     if (!gHasColorQD)
  251.         return 1;
  252.     
  253.     if (!theWindow)
  254.         return (**(**GetMainDevice()).gdPMap).pixelSize;
  255.     
  256.     thisHandle = GetDeviceList();
  257.     gBiggestDevice = 0L;
  258.     biggestSize = 0L;
  259.     
  260.     while (thisHandle)
  261.     {
  262.         if (TestDeviceAttribute(thisHandle, screenDevice) &&
  263.             TestDeviceAttribute(thisHandle, screenActive))
  264.         {
  265.             if (SectRect(&(theWindow->portRect),
  266.                     &((**thisHandle).gdRect), &tempRect))
  267.             {
  268.                 if (biggestSize < (tempSize = ((long)(tempRect.bottom - tempRect.top))*
  269.                     ((long)(tempRect.right - tempRect.left))))
  270.                 {
  271.                     biggestSize = tempSize;
  272.                     gBiggestDevice = thisHandle;
  273.                 }
  274.             }
  275.         }
  276.         thisHandle = GetNextDevice(thisHandle);
  277.     }
  278.     
  279.     return (gBiggestDevice) ? (**(**gBiggestDevice).gdPMap).pixelSize : 1;
  280. }
  281.  
  282. static    short GetWindowRealDepth(WindowPtr theWindow)
  283. {
  284.     return (gHasColorQD) ?
  285.                 ((theWindow) ?
  286.                     GetBiggestDeviceDepth(theWindow) :
  287.                     (**(**GetMainDevice()).gdPMap).pixelSize) :
  288.             1;
  289. }
  290.